package com.chengqj.blogweb.blog.repo.dao;

import com.chengqj.blogweb.blog.repo.pojo.TBlog;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface TBlogMapper {
    @Delete({
        "delete from t_blog",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into t_blog (img_url, title, ",
        "preface, type, ",
        "gmt_create, context)",
        "values (#{imgUrl,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, ",
        "#{preface,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{contextUrl,jdbcType=VARCHAR}, ",
        "#{gmtCreate,jdbcType=TIMESTAMP}, #{context,jdbcType=LONGVARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(TBlog record);

    @Select({
        "select",
        "id, img_url, title, preface, type,  gmt_create, context",
        "from t_blog",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="img_url", property="imgUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="preface", property="preface", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="context", property="context", jdbcType=JdbcType.LONGVARCHAR)
    })
    TBlog selectByPrimaryKey(Integer id);

    @Select({
            "select",
            "id, img_url, title, preface, type, gmt_create, context",
            "from t_blog",
            "where type = #{type,jdbcType=VARCHAR}" +
                    "order by gmt_create desc"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="img_url", property="imgUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="preface", property="preface", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
            @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="context", property="context", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<TBlog> selectByType(String type);

    @Select({
        "select",
        "id, img_url, title, preface, type,  gmt_create, context",
        "from t_blog order by gmt_create desc"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="img_url", property="imgUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="preface", property="preface", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
        @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="context", property="context", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<TBlog> selectAll();

    @Update({
        "update t_blog",
        "set img_url = #{imgUrl,jdbcType=VARCHAR},",
          "title = #{title,jdbcType=VARCHAR},",
          "preface = #{preface,jdbcType=VARCHAR},",
          "type = #{type,jdbcType=VARCHAR},",
          "gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},",
          "context = #{context,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(TBlog record);
}